<?php


namespace app\admin\controller;


use app\admin\service\RbacService;
use app\common\controller\CommonController;
use app\common\enum\MainEnum;
use app\common\exception\ParameterFailedException;
use app\common\exception\NotFoundException;
use app\common\model\MapModel;
use app\common\model\XXXXXModel;
use app\common\service\Lang;
use app\common\validate\XXXXXValidate;
use think\facade\Cache;
use think\facade\Db;

class MapController extends CommonController
{
    public function getMapList()
    {
        $data = Cache::store('file')
            ->get('map');
        if (!$data) {
            $models = (new MapModel())->getMapList();
            if ($models->isEmpty()) {
                $data = [];
            }
            else {
                $data = $models->toArray();
            }
            Cache::store('file')
                ->set('map', $data);
        }
        return retJson(Lang::GETDone, getPidParent($data));
    }

    public function postMap()
    {
        $params = self::getParam(['test']);
        $data   = json_decode($params['test'], true);
        foreach ($data as $province => $item) {
            $pModel = (new MapModel())->where('name', $province)
                ->where('type', MainEnum::MAP_TYPE_PROVINCE)
                ->find();
            if (!$pModel) {
                $pModel = new MapModel();
                $pModel->save([
                    'name' => $province,
                    'type' => MainEnum::MAP_TYPE_PROVINCE,
                ]);
            }
            foreach ($item as $city => $areas) {
                $cModel = (new MapModel())->where('name', $city)
                    ->where('type', MainEnum::MAP_TYPE_CITY)
                    ->where('pid', $pModel->getData('id'))
                    ->find();
                if (!$cModel) {
                    $cModel = new MapModel();
                    $cModel->save([
                        'name' => $city,
                        'type' => MainEnum::MAP_TYPE_CITY,
                        'pid'  => $pModel->getData('id'),
                    ]);
                }
                foreach ($areas as $area) {
                    $aModel = (new MapModel())->where('name', $area)
                        ->where('type', MainEnum::MAP_TYPE_AREA)
                        ->where('pid', $cModel->getData('id'))
                        ->find();
                    if (!$aModel) {
                        $aModel = new MapModel();
                        $aModel->save([
                            'name' => $area,
                            'type' => MainEnum::MAP_TYPE_AREA,
                            'pid'  => $cModel->getData('id'),
                        ]);
                    }
                }
            }
        }
    }

    public function putXXXXX()
    {

        $params = self::getParam([
            'id',
            'xxxxx',
        ]);
        (new XXXXXValidate())->goCheck(__FUNCTION__, $params);
        Db::startTrans();
        try {
            $model = XXXXXModel::where('id', $params['id'])
                ->find();
            if (!$model) {
                throw new NotFoundException();
            }

            $carModel = YYYYYModel::where('driver_id', $params['id'])
                ->find();
            if ($carModel) {
                throw new NotFoundException(['msg' => '该xx已被yy绑定，请解除绑定后继续操作']);
            }

            $model->nickname = $params['nickname'];
            $model->save();

            Db::commit();
            return retJson(Lang::PUTDone);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw $e;
        }
    }

    public function deleteXXXXX()
    {

        $params = self::getParam(['id']);
        (new XXXXXValidate())->goCheck(__FUNCTION__, $params);
        Db::startTrans();
        try {
            $model = XXXXXModel::where('id', $params['id'])
                ->find();
            if (!$model) {
                throw new NotFoundException();
            }


            $otherModel = OtherModel::where('id', $params['id'])
                ->find();
            if (!$otherModel) {
                throw new NotFoundException();
            }

            $model->delete_time = NOW_TIME;
            $model->save();

            Db::commit();

            return retJson(Lang::DELETEDone);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw $e;
        }
    }
}